package com.tunnel.client.net;

import com.tunnel.common.protocol.Protocol;
import com.tunnel.common.protocol.ProtocolType;
import com.tunnel.common.util.SequenceUtil;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.CharsetUtil;

/**
 * 本地转服务务
 */
public class LocalForward {
    public static void sendLocalRequest(String host, int port, int commonType, long sequenceId, String msg, Channel channel) throws Exception {

        EventLoopGroup group = new NioEventLoopGroup(1);
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(
                                    new SimpleChannelInboundHandler<ByteBuf>() {
                                        @Override
                                        protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
                                            String msgtmp = msg.toString(CharsetUtil.UTF_8);
                                            System.out.println("本地回复消息: " + msgtmp);
                                            Protocol protocol = new Protocol();
                                            protocol.setCommonType(commonType);
                                            protocol.setSequenceId(sequenceId);
                                            protocol.setLength(msgtmp.getBytes().length);
                                            protocol.setBody(msgtmp);
                                            channel.writeAndFlush(protocol);
                                            msg.release();
                                            ctx.close();
                                        }

                                        @Override
                                        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                                            cause.printStackTrace();
                                            ctx.close();
                                        }
                                    }
                            );
                        }
                    });
            // 绑定对端的地址和端口
            ChannelFuture f = b.connect(host, port).sync();
            // 构造ByteBuf
            ByteBuf requestBuf = Unpooled.copiedBuffer(msg.toString(), CharsetUtil.UTF_8);
            // 发送请求
            f.channel().writeAndFlush(requestBuf);
            f.channel().closeFuture().sync();

        } finally {
            group.shutdownGracefully();
        }

    }
}
